package cn.le.controller;

import cn.le.beans.dto.CrawlerDto;
import cn.le.beans.dto.CrawlerHomeSearchDto;
import cn.le.beans.vo.PageVo;
import cn.le.beans.vo.ResponseVO;
import cn.le.service.CrawlerHomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Description 阿里房源
 * @Author le
 * @Date 2020/2/22 21:02
 * @Version 0.1
 **/
@RestController
@RequestMapping(value = "/crawler")
@Api(value = "爬虫房源")
public class CrawlerHomeController {

    @Autowired
    private CrawlerHomeService crawlerHomeService;

    @ApiOperation(value = "爬取房源")
    @PostMapping(value = "/home")
    public ResponseVO crawler(@RequestBody @Valid CrawlerDto req) throws Exception {
        return ResponseVO.success(crawlerHomeService.crawler(req),null);
    }

    @ApiOperation(value = "爬取代理ip")
    @PostMapping(value = "/proxy")
    public ResponseVO proxy() throws Exception {
        return ResponseVO.success(crawlerHomeService.proxy(),null);
    }



    @ApiOperation(value = "ip代理")
    @GetMapping(value = "/ip")
    public ResponseVO ipProxy() throws Exception {
        return ResponseVO.success(crawlerHomeService.ipProxy(),null);
    }

    @ApiOperation(value = "爬虫房源分页")
    @PostMapping(value = "/page")
    public ResponseVO page(@RequestBody CrawlerHomeSearchDto req) throws Exception {
        return ResponseVO.success("ok",PageVo.restPage(crawlerHomeService.page(req)));
    }

    @ApiOperation(value = "爬虫房源分页")
    @PostMapping(value = "/todayNewHome")
    public ResponseVO todayNewHome() throws Exception {
        return ResponseVO.success("ok",crawlerHomeService.todayNewHome());
    }

    @ApiOperation(value = "下载房源图片")
    @GetMapping(value = "/download/homeImg/{id}")
    public void downloadHomeImg(HttpServletResponse response,@PathVariable("id")Long id) throws Exception {
        crawlerHomeService.downloadHomeImg(response,id);
    }
}
